How to solve these coding challenges:
Write your solution in PyCharm or in your preferred Python IDE.
If your solution is not correct, then try to understand the error messages, watch the video again, rewrite the solution, and test it again. Repeat this step until you get the correct solution.
Save the solution in a file for future reference or recap.
Challenge #1
Create a Python script that removes all the occurrences of a given element of a list.
Are you stuck? Do you want to see the solution to this exercise? Click here.
Challenge #2
Create a Python script that removes all the elements of a list that are duplicates.
Are you stuck? Do you want to see the solution to this exercise? Click here.
Challenge #3
Consider the following string: nums = '10,20,30,40,50'
Create a Python script that creates a list of integers from the string.
The resulting list will be: [10, 20, 30, 40, 50]
Are you stuck? Do you want to see the solution to this exercise? Click here.
Challenge #4
Write a Python script that finds all numbers that are divisible by 7 but are not a multiple of 5, between 1500 and 3200 (both included).
The numbers obtained should be printed in a comma-separated sequence (CSV) on a single line.
Are you stuck? Do you want to see the solution to this exercise? Click here.
Challenge #5
Write a program that prompts the user for a long string containing multiple words separated by whitespaces and prints back the same string with the words in backward order.
For example, say I type the string: My name is Andrei
Then the script should print out: Andrei is name My
Are you stuck? Do you want to see the solution to this exercise? Click here.
Challenge #6
Write a Python program that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically.
Sample input string : green-red-yellow-black-white
Expected Result : black-green-red-white-yellow
Are you stuck? Do you want to see the solution to this exercise? Click here.
Challenge #7
Write a Python program that accepts as input a sequence of words separated by one or more whitespaces and prints out the same words with the letters in reversed order. Do not use list comprehension.
Sample input string: I love cat and dogs
Expected Result: I evol tac dna sgod
Are you stuck? Do you want to see the solution to this exercise? Click here.
Challenge #8
Create an alternative solution for the previous challenge using list comprehension.
Are you stuck? Do you want to see the solution to this exercise? Click here.
Challenge #9
Create a Python script that calculates and displays the number of occurrences of each element of a list.
Sample list: list('mamma mia mm')
Expected Result:
m ---> 6
a ---> 3
---> 2
i ---> 1
Are you stuck? Do you want to see the solution to this exercise? Click here.
Challenge #10
Consider a list of words(strings). Write a Python script that generates a list of tuples where the first element of the tuple is the word in the list and the second element is its length.
Use list comprehension if possible.
Sample List: words = ['Python', 'Java', 'C++', 'Golang', 'Solidity', 'Bash']
Expected Result: [('Python', 6), ('Java', 4), ('C++', 3), ('Golang', 6), ('Solidity', 8), ('Bash', 4)]
Are you stuck? Do you want to see the solution to this exercise? Click here.